﻿using ExamRoomAllocationManagement.Dao;
using Xmu.Stores.Models;

namespace ExamRoomAllocationManagement.Services
{
    public class ExamRoomService : ISingletonDependency
    {
        private IServiceProvider serviceProvider;
        private Repositories repo;
        public ExamRoomService(IServiceProvider serviceProvider)
        {
            this.serviceProvider = serviceProvider;
            repo = serviceProvider.CreateScope().ServiceProvider.GetRequiredService<Repositories>();
        }

        public (object examRooms, object msg) GetExamRooms(int pageIndex, int pageSize)
        {
            return (repo.ExamRooms.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(), "ok");
        }

        public (object examRooms, object msg) GetExamRoom(Guid id)
        {
            return (repo.ExamRooms.FirstOrDefault(er => er.ExamRoomId == id), "ok");
        }

        public (bool success, string msg) AddExamRoom(ExamRoom examRoom)
        {
            repo.ExamRooms.Add(examRoom);
            repo.DbContex.SaveChanges();
            return (true, "ok");
        }

        internal (bool success, string msg) DeleteExamRoom(Guid id)
        {
            ExamRoom? examRoom = repo.ExamRooms.FirstOrDefault(er => er.ExamRoomId == id);
            if(examRoom == null) return (false, "examroom not found");
            repo.ExamRooms.Remove(examRoom);
            repo.DbContex.SaveChanges();
            return (true, "ok");
        }
    }
}